import * as Cesium from "cesium"
import flowImg from "./colors3.png"
const DEF_OPT = {
  color: Cesium.Color.BLACK, 
  duration: 3000, 
  count: 3.0, 
  freely: 'vertical',
  direction: '-'
}
var materialFlowLine = class {
  constructor(options) {
    options = options || DEF_OPT
    this._definitionChanged = new Cesium.Event();
    this._color = undefined;
    this._colorSubscription = undefined;
    this.color = options.color;
    this.duration = options.duration;
    this._time = new Date().getTime();
    this.create(options)
  }
  get isConstant() {
    return false
  }
  get definitionChanged() {
    return this._definitionChanged
  }
  getType(time) {
    return "PolylineTrailLink"
  }
  getValue (time, result) {
    if (!Cesium.defined(result)) {
      result = {};
    }
    result.color = Cesium.Property.getValueOrClonedDefault(
      this._color,
      time,
      Cesium.Color.WHITE,
      result.color
    );
    result.image = Cesium.Material.PolylineTrailLinkImage;
    result.time =
      ((new Date().getTime() - this._time) % this.duration) / this.duration;
    return result;
  };
  equals(other) {
    return (
      this === other ||
      (other instanceof materialFlowLine &&
        Cesium.Property.equals(this._color, other._color))
    );
  }
  getMaterail(options) {
    let materail = "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
    { czm_material material = czm_getDefaultMaterial(materialInput); vec2 st = materialInput.st;\n\
       vec4 colorImage = texture2D(image, vec2(fract(st.s - time), st.t));\n\
        material.alpha = colorImage.a * color.a;\n\
        material.diffuse = (colorImage.rgb + color.rgb) / 2.0;\n\
        return material;}";
    return materail
  }

  create(options) {
    Cesium.Material.PolylineTrailLinkType = "PolylineTrailLink";
    Cesium.Material.PolylineTrailLinkImage = options.image || flowImg; //图片
    Cesium.Material.PolylineTrailLinkSource = this.getMaterail(options)
    Cesium.Material._materialCache.addMaterial(
      Cesium.Material.PolylineTrailLinkType,{
        fabric: {
          type: Cesium.Material.PolylineTrailLinkType,
          uniforms: {
            color: new Cesium.Color(1.0, 1.0, 0.0, 0.5),
            image: Cesium.Material.PolylineTrailLinkImage,
            time: 20
          },
          source: Cesium.Material.PolylineTrailLinkSource
        },
        translucent: function (material) {
          return true;
        }
      }
    );
  }
}

Object.defineProperties(materialFlowLine.prototype, {
  color: Cesium.createPropertyDescriptor("color")
});
export default materialFlowLine